﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DetectApp
{
    public class HalconFun
    {
        public static void CalibX5(HObject ho_image, out HRegion hRegion1, out HRegion hRegion2, out HTuple hv_distance)
        {
            HObject ho_X5, ho_GrayImage, ho_ImageScaled;
            HObject ho_ImageResult, ho_ImageMean, ho_RegionLines, ho_RegionLines1;

            // Local control variables 

            HTuple hv_MetrologyHandle = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Parameter = new HTuple(), hv_MetrologyHandle1 = new HTuple();
            HTuple hv_Parameter1 = new HTuple(), hv_DistanceMin = new HTuple();
            HTuple hv_DistanceMax = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_X5);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_ImageResult);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);

            ho_X5.Dispose();

            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_image, out ho_GrayImage);
            ho_ImageScaled.Dispose();
            HOperatorSet.ScaleImage(ho_GrayImage, out ho_ImageScaled, 3, 0);
            ho_ImageResult.Dispose();
            HOperatorSet.MultImage(ho_ImageScaled, ho_ImageScaled, out ho_ImageResult, 0.005,
                0);
            ho_ImageMean.Dispose();
            HOperatorSet.MeanImage(ho_ImageResult, out ho_ImageMean, 3, 3);

            hv_MetrologyHandle.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);


            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, 790, 740, 1100,
                740, 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index);

            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle);

            hv_Parameter.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type",
                "all_param", out hv_Parameter);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                ho_RegionLines.Dispose();

                hRegion1 = new HRegion();
                hRegion1.GenRegionLine(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3));

            }

            //线2
            hv_MetrologyHandle1.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle1);
            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle1, 790, 2135, 1100,
                2135, 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index);
            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle1);

            hv_Parameter1.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle1, 0, "all", "result_type",
                "all_param", out hv_Parameter1);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion2 = new HRegion();
                hRegion2.GenRegionLine(hv_Parameter1.TupleSelect(0),
                    hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3));
            }

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_DistanceMin.Dispose(); hv_DistanceMax.Dispose();
                HOperatorSet.DistanceSs(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3), hv_Parameter1.TupleSelect(
                    0), hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3), out hv_DistanceMin, out hv_DistanceMax);
            }
            ho_X5.Dispose();
            ho_GrayImage.Dispose();
            ho_ImageScaled.Dispose();
            ho_ImageResult.Dispose();
            ho_ImageMean.Dispose();
            ho_RegionLines.Dispose();
            ho_RegionLines1.Dispose();

            hv_MetrologyHandle.Dispose();
            hv_Index.Dispose();
            hv_Parameter.Dispose();
            hv_MetrologyHandle1.Dispose();
            hv_Parameter1.Dispose();
            hv_DistanceMin.Dispose();
            hv_DistanceMax.Dispose();

            hv_distance = hv_DistanceMin;

        }



        public static void CalibX10(HObject ho_image, out HRegion hRegion1, out HRegion hRegion2, out HTuple hv_distance)
        {
            HObject ho_X5, ho_GrayImage, ho_ImageScaled;
            HObject ho_ImageResult, ho_ImageMean, ho_RegionLines, ho_RegionLines1;

            // Local control variables 

            HTuple hv_MetrologyHandle = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Parameter = new HTuple(), hv_MetrologyHandle1 = new HTuple();
            HTuple hv_Parameter1 = new HTuple(), hv_DistanceMin = new HTuple();
            HTuple hv_DistanceMax = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_X5);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_ImageResult);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);

            ho_X5.Dispose();

            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_image, out ho_GrayImage);
            ho_ImageScaled.Dispose();
            HOperatorSet.ScaleImage(ho_GrayImage, out ho_ImageScaled, 3, 0);
            ho_ImageResult.Dispose();
            HOperatorSet.MultImage(ho_ImageScaled, ho_ImageScaled, out ho_ImageResult, 0.005,
                0);
            ho_ImageMean.Dispose();
            HOperatorSet.MeanImage(ho_ImageResult, out ho_ImageMean, 3, 3);

            hv_MetrologyHandle.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);


            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, 640, 707, 844,
        707, 20, 5, 1, 15, new HTuple(), new HTuple(), out hv_Index);

            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle);

            hv_Parameter.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type",
                "all_param", out hv_Parameter);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion1 = new HRegion();
                hRegion1.GenRegionLine(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3));
            }

            //线2
            hv_MetrologyHandle1.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle1);
            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle1, 630, 1824, 800,
        1824, 20, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index);
            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle1);

            hv_Parameter1.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle1, 0, "all", "result_type",
                "all_param", out hv_Parameter1);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion2 = new HRegion();
                hRegion2.GenRegionLine(hv_Parameter1.TupleSelect(0),
                    hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3));
            }

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_DistanceMin.Dispose(); hv_DistanceMax.Dispose();
                HOperatorSet.DistanceSs(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3), hv_Parameter1.TupleSelect(
                    0), hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3), out hv_DistanceMin, out hv_DistanceMax);
            }
            ho_X5.Dispose();
            ho_GrayImage.Dispose();
            ho_ImageScaled.Dispose();
            ho_ImageResult.Dispose();
            ho_ImageMean.Dispose();
            ho_RegionLines.Dispose();
            ho_RegionLines1.Dispose();

            hv_MetrologyHandle.Dispose();
            hv_Index.Dispose();
            hv_Parameter.Dispose();
            hv_MetrologyHandle1.Dispose();
            hv_Parameter1.Dispose();
            hv_DistanceMin.Dispose();
            hv_DistanceMax.Dispose();

            hv_distance =hv_DistanceMin;
        }


        public static void CalibX20(HObject ho_image, out HRegion hRegion1, out HRegion hRegion2, out HTuple hv_distance)
        {
            HObject ho_X5, ho_GrayImage, ho_ImageScaled;
            HObject ho_ImageResult, ho_ImageMean, ho_RegionLines, ho_RegionLines1;

            // Local control variables 

            HTuple hv_MetrologyHandle = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Parameter = new HTuple(), hv_MetrologyHandle1 = new HTuple();
            HTuple hv_Parameter1 = new HTuple(), hv_DistanceMin = new HTuple();
            HTuple hv_DistanceMax = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_X5);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_ImageResult);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);

            ho_X5.Dispose();

            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_image, out ho_GrayImage);
            ho_ImageScaled.Dispose();
            HOperatorSet.ScaleImage(ho_GrayImage, out ho_ImageScaled, 3, 0);
            ho_ImageResult.Dispose();
            HOperatorSet.MultImage(ho_ImageScaled, ho_ImageScaled, out ho_ImageResult, 0.005,
                0);
            ho_ImageMean.Dispose();
            HOperatorSet.MeanImage(ho_ImageResult, out ho_ImageMean, 3, 3);

            hv_MetrologyHandle.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);


            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, 355, 626, 1017, 626, 60, 5, 1, 15, new HTuple(), new HTuple(), out hv_Index);

            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle);

            hv_Parameter.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type",
                "all_param", out hv_Parameter);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion1 = new HRegion();
                hRegion1.GenRegionLine(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3));
            }

            //线2
            hv_MetrologyHandle1.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle1);
            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle1, 459, 1741, 935, 1750, 60, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index);
            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle1);

            hv_Parameter1.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle1, 0, "all", "result_type",
                "all_param", out hv_Parameter1);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion2 = new HRegion();
                hRegion2.GenRegionLine(hv_Parameter1.TupleSelect(0),
                    hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3));
            }

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_DistanceMin.Dispose(); hv_DistanceMax.Dispose();
                HOperatorSet.DistanceSs(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3), hv_Parameter1.TupleSelect(
                    0), hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3), out hv_DistanceMin, out hv_DistanceMax);
            }
            ho_X5.Dispose();
            ho_GrayImage.Dispose();
            ho_ImageScaled.Dispose();
            ho_ImageResult.Dispose();
            ho_ImageMean.Dispose();
            ho_RegionLines.Dispose();
            ho_RegionLines1.Dispose();

            hv_MetrologyHandle.Dispose();
            hv_Index.Dispose();
            hv_Parameter.Dispose();
            hv_MetrologyHandle1.Dispose();
            hv_Parameter1.Dispose();
            hv_DistanceMin.Dispose();
            hv_DistanceMax.Dispose();

            hv_distance = hv_DistanceMin;
        }



        public static void CalibX50(HObject ho_image, out HRegion hRegion1, out HRegion hRegion2, out HTuple hv_distance)
        {
            HObject ho_X5, ho_GrayImage, ho_ImageScaled;
            HObject ho_ImageResult, ho_ImageMean, ho_RegionLines, ho_RegionLines1;

            // Local control variables 

            HTuple hv_MetrologyHandle = new HTuple(), hv_Index = new HTuple();
            HTuple hv_Parameter = new HTuple(), hv_MetrologyHandle1 = new HTuple();
            HTuple hv_Parameter1 = new HTuple(), hv_DistanceMin = new HTuple();
            HTuple hv_DistanceMax = new HTuple();
            // Initialize local and output iconic variables 
            HOperatorSet.GenEmptyObj(out ho_X5);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_ImageScaled);
            HOperatorSet.GenEmptyObj(out ho_ImageResult);
            HOperatorSet.GenEmptyObj(out ho_ImageMean);
            HOperatorSet.GenEmptyObj(out ho_RegionLines);
            HOperatorSet.GenEmptyObj(out ho_RegionLines1);

            ho_X5.Dispose();

            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_image, out ho_GrayImage);
            ho_ImageScaled.Dispose();
            HOperatorSet.ScaleImage(ho_GrayImage, out ho_ImageScaled, 3, 0);
            ho_ImageResult.Dispose();
            HOperatorSet.MultImage(ho_ImageScaled, ho_ImageScaled, out ho_ImageResult, 0.005,
                0);
            ho_ImageMean.Dispose();
            HOperatorSet.MeanImage(ho_ImageResult, out ho_ImageMean, 3, 3);

            hv_MetrologyHandle.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle);


            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle, 370, 507, 1500,
        507, 60, 5, 1, 15, new HTuple(), new HTuple(), out hv_Index);

            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle);

            hv_Parameter.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle, 0, "all", "result_type",
                "all_param", out hv_Parameter);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion1 = new HRegion();
                hRegion1.GenRegionLine(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3));
            }

            //线2
            hv_MetrologyHandle1.Dispose();
            HOperatorSet.CreateMetrologyModel(out hv_MetrologyHandle1);
            hv_Index.Dispose();
            HOperatorSet.AddMetrologyObjectLineMeasure(hv_MetrologyHandle1, 361, 1910, 1425,
        1930, 60, 5, 1, 30, new HTuple(), new HTuple(), out hv_Index);
            HOperatorSet.ApplyMetrologyModel(ho_ImageMean, hv_MetrologyHandle1);

            hv_Parameter1.Dispose();
            HOperatorSet.GetMetrologyObjectResult(hv_MetrologyHandle1, 0, "all", "result_type",
                "all_param", out hv_Parameter1);
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hRegion2 = new HRegion();
                hRegion2.GenRegionLine(hv_Parameter1.TupleSelect(0),
                    hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3));
            }

            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
                hv_DistanceMin.Dispose(); hv_DistanceMax.Dispose();
                HOperatorSet.DistanceSs(hv_Parameter.TupleSelect(0), hv_Parameter.TupleSelect(
                    1), hv_Parameter.TupleSelect(2), hv_Parameter.TupleSelect(3), hv_Parameter1.TupleSelect(
                    0), hv_Parameter1.TupleSelect(1), hv_Parameter1.TupleSelect(2), hv_Parameter1.TupleSelect(
                    3), out hv_DistanceMin, out hv_DistanceMax);
            }
            ho_X5.Dispose();
            ho_GrayImage.Dispose();
            ho_ImageScaled.Dispose();
            ho_ImageResult.Dispose();
            ho_ImageMean.Dispose();
            ho_RegionLines.Dispose();
            ho_RegionLines1.Dispose();

            hv_MetrologyHandle.Dispose();
            hv_Index.Dispose();
            hv_Parameter.Dispose();
            hv_MetrologyHandle1.Dispose();
            hv_Parameter1.Dispose();
            hv_DistanceMin.Dispose();
            hv_DistanceMax.Dispose();

            hv_distance = hv_DistanceMin;
        }
    }
}
